
package com.wwy.monitor.controller;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.common.PageResult;
import com.wwy.monitor.common.ResponseEnum;
import com.wwy.monitor.common.ServiceResponse;
import com.wwy.monitor.model.enumeration.DeleteEnum;
import com.wwy.monitor.model.tddl.dto.MonitorConfigIpRelDTO;
import com.wwy.monitor.model.tddl.dto.MonitorReceiverDTO;
import com.wwy.monitor.model.tddl.query.MonitorReceiverQuery;
import com.wwy.monitor.model.vo.ReceiverBatchVO;
import com.wwy.monitor.service.MonitorReceiverService;
import com.wwy.monitor.util.UserUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/api/monitorReceiver")
public class MonitorReceiverController {
    @Resource(name = "monitorReceiverService")
    private MonitorReceiverService monitorReceiverService;

    /**
     * 分页获取
     *
     * @return
     */
    @GetMapping("/page")
    @ResponseBody
    private ServiceResponse page(Integer pageNum, Integer pageSize, String empId, Long configId) {
        if (pageNum == null || pageSize == null) {
            return ServiceResponse.paramIllegal("分页参数不能为空");
        }
        MonitorReceiverQuery query = new MonitorReceiverQuery();
        if (StringUtils.isNotBlank(empId)) {
            query.setEmpId(empId);
        }
        if (configId != null) {
            query.setConfigId(configId);
        }
        Integer offset = (pageNum - 1) * pageSize;
        query.setOffset(offset);
        query.setSize(pageSize);
        query.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        PageResult pageResult = new PageResult();
        pageResult.setCurrentPage(pageNum);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalCount(monitorReceiverService.count(query));
        pageResult.setDatas(monitorReceiverService.selectByQuery(query));
        pageResult.getPages();
        return ServiceResponse.success(pageResult);
    }

    @PostMapping("/add")
    @ResponseBody
    private ServiceResponse add(HttpServletRequest request, @RequestBody MonitorReceiverDTO dto)
            throws IOException, ServletException {
        // 参数校验
        if (!checkParameter(dto)) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        // todo: ipv4 ipv6校验
        String empId = UserUtils.getBucEmpId(request);
        dto.setCreateUser(empId);
        dto.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
        boolean flag = monitorReceiverService.insert(dto);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.SAVE_EXCEPTION.getCode(), ResponseEnum.SAVE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }

    @PostMapping("/addBatch")
    @ResponseBody
    private ServiceResponse addBatch(@RequestBody ReceiverBatchVO vo, HttpServletRequest request)
            throws IOException, ServletException {
        if (null == vo.getDetailId() || vo.getDetailId() <= 0) {
            throw new MessageException("非法id");
        }
        int count = monitorReceiverService.deleteByDetailId(vo.getDetailId());
        String empId = UserUtils.getBucEmpId(request);
        List<MonitorReceiverDTO> data = Lists.newArrayList();
        if (null != vo.getReceiverList() && vo.getReceiverList().size() > 0) {
            for (MonitorReceiverDTO dto : vo.getReceiverList()) {
                dto.setDetailId(vo.getDetailId());
                dto.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
                dto.setCreateUser(empId);
                dto.setUpdateUser(empId);
                dto.setCreateTime(new Date());
                dto.setUpdateTime(new Date());
                data.add(dto);
            }
            return ServiceResponse.success(monitorReceiverService.batchInsert(data));
        } else {
            return ServiceResponse.success(count);
        }
    }

    @PostMapping("/update")
    @ResponseBody
    private ServiceResponse update(HttpServletRequest request, @RequestBody MonitorReceiverDTO dto)
            throws IOException, ServletException {
// 参数校验
        if (!checkParameter(dto)) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        String empId = UserUtils.getBucEmpId(request);
        dto.setUpdateUser(empId);
        boolean flag = monitorReceiverService.updateById(dto);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.UPDATE_EXCEPTION.getCode(), ResponseEnum.UPDATE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }

    /**
     * 校验参数
     *
     * @param dto
     * @return
     */
    private boolean checkParameter(MonitorReceiverDTO dto) {
        return dto != null && dto.getEmpId() != null && dto.getDetailId() != null && dto.getType() != null
                && dto.getAlertType() != null && StringUtils.isNotBlank(dto.getAddress());
    }

    @PostMapping("/delete")
    @ResponseBody
    private ServiceResponse delete(Long id) {
        if (id == null) {
            return ServiceResponse.fail(ResponseEnum.PARAM_EMPTY.getCode(), ResponseEnum.PARAM_EMPTY.getDesc());
        }
        boolean flag = monitorReceiverService.deleteById(id);
        if (!flag) {
            return ServiceResponse.fail(ResponseEnum.DELETE_EXCEPTION.getCode(), ResponseEnum.DELETE_EXCEPTION.getDesc());
        }
        return ServiceResponse.success(ResponseEnum.SUCCESS.getDesc());
    }
}